/**
 * all Function
 */

$(document)
		.ready(
				function() {

					bodytable$ = $("#example").dataTable({
						'iDisplayLength' : 15,
					});
					// hide some unused
					$('.dataTables_length').hide();
					$('.dataTables_info').hide();
					$('.dataTables_filter').hide();
					// filter only active Region
					oTable = $("#example").dataTable();
					oTable.fnFilter('Yes', 3);
					$('#inactive').prop('checked', false);
					// //////////////////////////////////////
					var table = $('#example').DataTable();

					$('#example tbody')
							.on(
									'click',
									'tr',
									function() {
										if ($(this).hasClass('selected')) {
											$(this).removeClass('selected');
										} else {
											table.$('tr.selected').removeClass(
													'selected');
											$(this).addClass('selected');
										}

										// amend
										// alert("clicked");
										var nTds = $('td', this);
										var inactive = $(nTds[3]).text();
										var regionName = $(nTds[0]).text();
										var description = $(nTds[1]).text();
										var countryName = $(nTds[2]).text();
										if (inactive === "No") {
											var cf = confirm("Do you want to make this Trust Region active?");
											if (cf === true) {
												// make this region active and
												// amend this region
												$
														.ajax({
															type : "POST",
															url : "/group3/makeActiveRegion",
															data : "countryName="
																	+ countryName
																	+ "&regionName="
																	+ regionName
																	+ "&description="
																	+ description,
															dataType : "json",
															success : function(
																	data) {
																// Check if
																// response is
																// success.
																if (data.result == 1) {
																	window.location.href = "/group3/TrustRegionDetail";
																} else {
																	alert(data.message);
																}
															},
															error : function(
																	result) {
																alert("error make active");
															}
														});
											}
										} else {
											// amend region
											$
													.ajax({
														type : "POST",
														url : "/group3/makeActiveRegion",
														data : "countryName="
																+ countryName
																+ "&regionName="
																+ regionName
																+ "&description="
																+ description,
														dataType : "json",
														success : function(data) {
															// Check if response
															// is success.
															if (data.result == 1) {
																window.location.href = "/group3/TrustRegionDetail";
															} else {
																alert(data.message);
															}
														},
														error : function(result) {
															alert("error make active");
														}
													});
										}
									});
				});

$(function() {
	// Event when click checkbox
	$("#inactive").click(function() {
		// Check value
		if ($(this).is(":checked")) {
			oTable = $("#example").dataTable();
			oTable.fnFilter('', 3);

		} else {
			oTable = $("#example").dataTable();
			oTable.fnFilter('Yes', 3);
		}
	});

	$("#hp_ae").click(
			function() {
				$('#example').DataTable().column(0)
						.search('^[A-E]', true, true).draw();

			});

	$("#hp_all").click(function() {
		$('#example').DataTable().column(0).search('.*', true, true).draw();
	});

	$("#hp_09").click(
			function() {
				$('#example').DataTable().column(0)
						.search('^[0-9]', true, true).draw();

			});
	$("#hp_fj").click(
			function() {
				$('#example').DataTable().column(0)
						.search('^[F-J]', true, true).draw();

			});
	$("#hp_ln").click(
			function() {
				$('#example').DataTable().column(0)
						.search('^[L-N]', true, true).draw();

			});
	$("#hp_or").click(
			function() {
				$('#example').DataTable().column(0)
						.search('^[O-R]', true, true).draw();

			});
	$("#hp_sv").click(
			function() {
				$('#example').DataTable().column(0)
						.search('^[S-V]', true, true).draw();

			});
	$("#hp_wz").click(
			function() {
				$('#example').DataTable().column(0)
						.search('^[W-Z]', true, true).draw();

			});
});

/**
 * Create new Trust Region
 */
function createRegion() {
	window.location = "AddTrustRegion.html";
}